const util = require('../../../../utils/util.js');
const api = require('../../../../config/api.js');
const user = require('../../../../services/user.js');
const common = require('../../../../utils/common.js');
const app = getApp()
Page({
  data: {
    //浏览模块
    openPanel: 0,
    //随笔行程类型浏览模块
    openDiaryType: 0,
    //随笔行程排序面板
    openDiaryPx: true,
    //随笔行程排序类型
    diaryPx: 0,
    //圈子行程排序面板
    openJourneyPx: true,
    //圈子行程排序类型
    journeyPx: 0,
    //圈子行程数据
    journeyList: [{ "journeyMove": false }, { "journeyMove": false }, { "journeyMove": false }, { "journeyMove": false }],
    //圈子行程统计面板
    openStatistical: null,
    //视频随笔数据
    videoDiaryList: [{ "type": 1, "path": "https://image.xingqubaike.com:8443/minicard/upload/diary/2019/07/15/15caeaf9-60c3-4262-81d7-d67c3860313f.jpg", "title": "你知道我在等你吗？你是否也会在乎我？嘻嘻你知道我在等你吗？你", "lable": ["旅游", "摄影"], "time": "1小时前", "circleid": 1 }],
    //美图随笔数据
    imgDiaryList: [{ "type": 2, "path": "https://image.xingqubaike.com:8443/minicard/upload/diary/2019/07/15/03030f1d-aec4-4a77-ae12-77101fa5eb39.jpg", "title": "一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十", "lable": ["美食·烹饪", "娱乐·乐器", "游戏·哈哈"], "time": "刚刚" },
    { "type": 2, "path": "https://image.xingqubaike.com:8443/minicard/upload/diary/2019/07/15/720e1022-1afc-412d-b7d8-f8b49d14a2ab.jpg", "title": "你知道我在等你吗？你是否也会在乎我？嘻嘻你知道我在等你吗？你", "lable": ["旅游", "摄影"], "time": "1天前" }],
    //草稿箱数据
    draftDiaryList: [],
    //上传面板
    openUpload: false,
    //上传路径面板
    openUploadPop: false,
    //日历的缩放
    openRili: false,
    //当前的年月
    newYearMonth: '',
    //当前日期
    newDate: '',
    //当前几号
    dateNum: '',
    //当前星期几
    dayNum: '',
    //当月最后一天
    lastDateNum: '',
    //当月第一天星期几
    actionDayNum: '',
  },
  onLoad: function (option) {
    var _this = this;
    _this.setData({
      logininfo: app.globalData.logininfo,
      openid: app.globalData.logininfo.openid,
    });
    //判断是不是用户本人访问
    if (option.openid != null && option.openid != '') {
      _this.setData({
        getopenid: option.openid
      })     
    } else {
      _this.setData({
        getopenid: app.globalData.logininfo.openid
      })
    }
    if (_this.data.getopenid == app.globalData.logininfo.openid){
      wx.setNavigationBarTitle({
        title: "我的行程日历",
      })
    }else{
      wx.setNavigationBarTitle({
        title: "Ta的行程日历",
      })
    }
    _this.getNewDate();
    var videoarr = _this.data.videoDiaryList
    if (videoarr != null && videoarr != '') {
      var selectInfo = videoarr[0]
      _this.setData({
        selectInfo: selectInfo
      })
    }
  },
  onReady: function () {
    var _this = this
    var openid = _this.data.openid
    var getopenid = _this.data.getopenid;
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.openid = getopenid;
        data.access_token = res.access_token;
        //获取用户信息
        util.post(api.GetUserInfoByHomepage, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              userData: res.result.json,
            })
            console.log(_this.data.userData)
          }
        });
        //获取用户资料
        util.post(api.GetHomeInfo, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              userData2: res.result.entity
            })
            console.log(_this.data.userData2)
          }
        });
        var data = {};
        data.openid = openid;
        data.selectOpenid = getopenid;
        data.access_token = res.access_token;
      });
    }
  },
  onShow: function () {
    var _this = this
  },
  //选择浏览面板
  selectPanel: function (e) {
    var _this = this
    var index = e.currentTarget.dataset.index
    _this.setData({
      openPanel: index
    })
  },
  //选择浏览随笔行程类型面板
  selectDiaryType: function (e) {
    var _this = this
    var index = e.currentTarget.dataset.index
    _this.setData({
      openDiaryType: index
    })
    if (index == 0) {
      var videoarr = _this.data.videoDiaryList
      if (videoarr != null && videoarr != '') {
        var selectInfo = videoarr[0]
        _this.setData({
          selectInfo: selectInfo
        })
      }
    }
    if (index == 1) {
      var imgarr = _this.data.imgDiaryList
      if (imgarr != null && imgarr != '') {
        var selectInfo = imgarr[0]
        _this.setData({
          selectInfo: selectInfo
        })
      }
    }
  },
  //打开随笔行程排序面板
  showDiaryPx: function () {
    var _this = this
    _this.setData({
      openDiaryPx: false
    })
  },
  //打开随笔行程排序面板
  showJourneyPx: function () {
    var _this = this
    var query = wx.createSelectorQuery();
    query.select('.rili-list').boundingClientRect(function (rect) {
      var wz = parseInt(rect.height + 40 + 30);
      _this.setData({
        openJourneyPx: false,
        wz: wz
      })
    }).exec()
  },
  //选择随笔行程排序
  selectDiaryPx: function (e) {
    var _this = this
    var index = e.currentTarget.dataset.index
    _this.setData({
      diaryPx: index,
      openDiaryPx: true
    })
  },
  //选择随笔行程排序
  selectJourneyPx: function (e) {
    var _this = this
    var index = e.currentTarget.dataset.index
    _this.setData({
      journeyPx: index,
      openJourneyPx: true
    })
  },
  //选中美图随笔
  selectDiary: function (e) {
    var _this = this
    var index = e.currentTarget.dataset.index
    var type = e.currentTarget.dataset.type
    if (type == "img") {
      var imgarr = _this.data.imgDiaryList
      var selectInfo = imgarr[index]
      _this.setData({
        selectInfo: selectInfo
      })
    }
    if (type == "video") {
      var videoarr = _this.data.videoDiaryList
      var selectInfo = videoarr[index]
      _this.setData({
        selectInfo: selectInfo
      })
    }
    console.log(selectInfo)
  },
  //打开上传面板
  showUpload: function () {
    var _this = this
    _this.setData({
      openUpload: true
    })

  },
  //打开选择上传路径面板
  showUploadPop: function (e) {
    var _this = this
    var type = e.currentTarget.dataset.type
    if (type == "img") {
      var str = [{ "title": "拍照", "type": "img" }, { "title": "从手机相册选择", "type": "img" }, { "title": "从微信导入图片", "type": "img" }]
      _this.setData({
        popText: str
      })
    }
    if (type == "video") {
      var str = [{ "title": "拍摄", "type": "video" }, { "title": "从手机相册选择", "type": "video" }, { "title": "从微信导入视频", "type": "video" }]
      _this.setData({
        popText: str
      })
    }
    this.setData({
      openUploadPop: true,
      openUpload: false
    })
  },
  //打开拍摄
  openShoot: function (e) {
    var _this = this
    var type = e.currentTarget.dataset.type
    if (type == "img") {
      var imgArr = []
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['camera'],
        success(res) {
          _this.closeMC();
          wx.navigateTo({
            url: '/ucenter/pages/ucenter/personinfo/avatar/index?atype=journeyImg&imageSrc=' + res.tempFilePaths[0],
          })
        }
      })
    }
    if (type == "video") {
      wx.chooseVideo({
        sourceType: ['camera'],
        maxDuration: util.getVideoFileMaxTime("diary"),
        camera: 'back',
        success(res) {
          _this.closeMC();
          var info = {};
          info.id = "myVideo" + new Date().getTime();
          info.size = res.size;
          info.path = res.tempFilePath;
          info.width = res.width;
          info.height = res.height;
          info.totalTime = res.duration;
          info.totalDate = util.digitalFormatTime(res.duration);
          wx.navigateTo({
            url: '/interest/pages/interest/makeImage/publishJourneyVideo/publishJourneyVideo?info=' + JSON.stringify(info),
          })
        }
      })
    }
  },
  //打开相册
  openAlbum: function (e) {
    var _this = this
    var type = e.currentTarget.dataset.type
    if (type == "img") {
      var imgArr = []
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album'],
        success(res) {
          _this.closeMC();
          wx.navigateTo({
            url: '/ucenter/pages/ucenter/personinfo/avatar/index?atype=journeyImg&imageSrc=' + res.tempFilePaths[0],
          })
        }
      })
    }
    if (type == "video") {
      wx.chooseVideo({
        sourceType: ['album'],
        maxDuration: util.getVideoFileMaxTime("diary"),
        camera: 'back',
        success(res) {
          _this.closeMC();
          var maxSize = util.getVideoFileMaxSize("diary");
          if ((res.size / 1024 / 1024) > maxSize) {
            util.dialogAlert("视频大小超出" + maxSize + "M");
            return;
          }
          var info = {};
          info.id = "myVideo" + new Date().getTime();
          info.size = res.size;
          info.path = res.tempFilePath;
          info.width = res.width;
          info.height = res.height;
          info.totalTime = res.duration;
          info.totalDate = util.digitalFormatTime(res.duration);
          wx.navigateTo({
            url: '/interest/pages/interest/makeImage/publishJourneyVideo/publishJourneyVideo?info=' + JSON.stringify(info),
          })
        }
      })
    }
  },
  //打开微信文件
  openWeChatFile: function (e) {
    var _this = this
    var type = e.currentTarget.dataset.type
    if (type == "img") {
      var imgArr = []
      wx.chooseMessageFile({
        count: 1,
        type: 'image',
        success(res) {
          _this.closeMC();
          wx.navigateTo({
            url: '/ucenter/pages/ucenter/personinfo/avatar/index?atype=journeyImg&imageSrc=' + res.tempFiles[0].path,
          })
        }
      })
    }
    if (type == "video") {
      wx.chooseMessageFile({
        count: 1,
        type: 'video',
        success(res) {
          var tempFiles = res.tempFiles[0];
          var maxSize = util.getVideoFileMaxSize("diary");
          if ((tempFiles.size / 1024 / 1024) > maxSize) {
            util.dialogAlert("视频大小超出" + maxSize + "M");
            return;
          }
          var info = {};
          info.id = "myVideo" + new Date().getTime();
          info.size = tempFiles.size;
          info.path = tempFiles.path;
          info.width = 0;
          info.height = 0;
          info.totalTime = 0;
          info.totalDate = "00:00";
          wx.navigateTo({
            url: '/interest/pages/interest/makeImage/publishJourneyVideo/publishJourneyVideo?info=' + JSON.stringify(info),
          })
        }
      })
    }
  },
  //获取当前时间
  getNewDate: function () {
    var _this = this;
    var d = new Date()
    var newDate = util.formatTime(d).substring(0, 10);
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var newYearMonth = year + "年" + month + "月"
    var dateNum = d.getDate()
    var dayNum = d.getDay()
    var d1 = new Date(year, month, 0);
    var lastDateNum = d1.getDate();
    var d2 = new Date(year, month - 1, 1);
    var actionDayNum = d2.getDay();
    _this.setData({
      newDate: newDate,
      newYearMonth: newYearMonth,
      dateNum: dateNum,
      dayNum: dayNum,
      lastDateNum: lastDateNum,
      actionDayNum: actionDayNum,
    });
  },
  //获取选择的时间
  bindDateChange: function (e) {
    var _this = this;
    var selDate = e.detail.value
    var newDate = new Date(selDate)
    var year = newDate.getFullYear();
    var month = newDate.getMonth() + 1;
    var newYearMonth = year + "年" + month + "月"
    var dateNum = newDate.getDate()
    var dayNum = newDate.getDay()
    var d1 = new Date(year, month, 0);
    var lastDateNum = d1.getDate();
    var d2 = new Date(year, month - 1, 1);
    var actionDayNum = d2.getDay();
    _this.setData({
      newDate: selDate,
      newYearMonth: newYearMonth,
      dateNum: dateNum,
      dayNum: dayNum,
      lastDateNum: lastDateNum,
      actionDayNum: actionDayNum,
    });
  },
  //跳转月份
  updateMonth: function (e) {
    var _this = this;
    var type = e.currentTarget.dataset.type
    var newDate = _this.data.newDate
    var d = new Date(newDate)
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    if (type == "up") {
      var d1 = new Date(year, month - 2, 1)
    }
    if (type == "down") {
      var d1 = new Date(year, month, 1)
    }
    var newDate = util.formatTime(d1).substring(0, 10);
    var year1 = d1.getFullYear();
    var month1 = d1.getMonth() + 1;
    var newYearMonth = year1 + "年" + month1 + "月"
    var dateNum = d1.getDate()
    var dayNum = d1.getDay()
    var d2 = new Date(year1, month1, 0);
    var lastDateNum = d2.getDate();
    var d3 = new Date(year1, month1 - 1, 1);
    var actionDayNum = d3.getDay();
    _this.setData({
      newDate: newDate,
      newYearMonth: newYearMonth,
      dateNum: dateNum,
      dayNum: dayNum,
      lastDateNum: lastDateNum,
      actionDayNum: actionDayNum,
    });
  },
  //选择某一天
  selectDate: function (e) {
    var _this = this;
    var date = e.currentTarget.dataset.date
    var newDate = _this.data.newDate
    var d = new Date(newDate)
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var d1 = new Date(year, month - 1, date)
    var newDate = util.formatTime(d1).substring(0, 10);
    var year1 = d1.getFullYear();
    var month1 = d1.getMonth() + 1;
    var newYearMonth = year1 + "年" + month1 + "月"
    var dateNum = d1.getDate()
    var dayNum = d1.getDay()
    var d2 = new Date(year1, month1, 0);
    var lastDateNum = d2.getDate();
    var d3 = new Date(year1, month1 - 1, 1);
    var actionDayNum = d3.getDay();
    _this.setData({
      newDate: newDate,
      newYearMonth: newYearMonth,
      dateNum: dateNum,
      dayNum: dayNum,
      lastDateNum: lastDateNum,
      actionDayNum: actionDayNum,
    });
  },
  //缩放日历模块
  showRili: function () {
    var _this = this;
    var open = _this.data.openRili
    if (open) {
      _this.setData({
        openRili: false
      });
    } else {
      _this.setData({
        openRili: true
      });
    }
  },
  //选择展开的统计
  showStatistical: function (e) {
    var _this = this
    var index = e.currentTarget.dataset.index
    var statistical = _this.data.openStatistical;
    if (statistical == index) {
      _this.setData({
        openStatistical: null,
      })
    } else {
      _this.setData({
        openStatistical: index,
      })
    }
  },
  //行程块拖动点击开始
  startMove: function (e) {
    var _this = this
    var index = e.currentTarget.dataset.index
    var arr = _this.data.journeyList
    var startX = e.changedTouches[0].clientX
    var startY = e.changedTouches[0].clientY
    for (var i = 0; i < arr.length; i++) {
      arr[i].journeyMove = false
    }
    _this.setData({
      journeyList: arr,
      startX: startX,
      startY: startY,
      startIndex: index,
    })
  },
  //行程块拖动点击结束
  endMove: function (e) {
    var _this = this
    var index = e.currentTarget.dataset.index
    var startX = _this.data.startX
    var startY = _this.data.startY
    var startIndex = _this.data.startIndex
    var endX = e.changedTouches[0].clientX
    var endY = e.changedTouches[0].clientY
    if (startIndex == index) {
      if ((startX - endX) > 50) {
        var arr = _this.data.journeyList
        arr[index].journeyMove = true
        _this.setData({
          journeyList: arr,
        })
      }
    }
  },
  //打开圈子行程功能
  showJourneyFunction: function (e) {
    var _this = this
    var index = e.currentTarget.dataset.index
    var arr = _this.data.journeyList
    arr[index].journeyMove = true
    _this.setData({
      journeyList: arr,
    })
  },
  //关闭面板
  closeMC: function () {
    var _this = this
    _this.setData({
      openDiaryPx: true,
      openUpload: false,
      openUploadPop: false,
      openJourneyPx: true,
    })
  },
  //跳转兴趣圈
  gotoInterest: function(){
    wx.switchTab({
      url: '/pages/interest/interest',
    })
  },
  //跳转首页
  gotoHome: function () {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
})